package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;

import com.foreknow.elm.util.CommonUtil;

import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int insert(Orders orders) throws SQLException {
        conn = DBUtil.getConnection();
        //订购日期需要主动生成

        String date = CommonUtil.getCurrentDate();
        String sql = "insert into orders (userId,businessId,orderDate,daId,orderTotal) values (?,?,?,?,?)";
        pst = conn.prepareStatement(sql);
        String userid = orders.getUserId();
        Integer bid = orders.getBusinessId();
        Integer daid = orders.getDaId();
        double total = orders.getOrderTotal();
        pst.setString(1,userid);
        pst.setInt(2,bid);

        pst.setString(3,date);
        pst.setInt(4,daid);
        pst.setDouble(5,total);
        int a = 0;
        int orderid = 0;
        try {
            a = pst.executeUpdate();
//            rs = pst.getGeneratedKeys();
//            if (rs.next()){
//                orderid = rs.getInt("orderId");
//            }
            //添加成功
            /**
             *  1.并获取自动生成的订单编号，
             */
            String sql1 = "select orderId from orders where userId=? and businessId=? and orderDate=? and daId=? and orderTotal=?";
            pst = conn.prepareStatement(sql1);
            pst.setString(1,userid);
            pst.setInt(2,bid);
            pst.setString(3,date);
            pst.setInt(4,daid);
            pst.setDouble(5,total);
            rs = pst.executeQuery();

            while (rs.next()){
                orderid = rs.getInt("orderId");
            }
            /**
             *2.然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
             *3.然后根据用户编号、商家编号删除购物车表中的数据。
             */

        }finally {
            DBUtil.close(rs,pst);
        }
        return orderid;
    }

    @Override
    public Orders queryobo(Integer orderId) throws SQLException {
        conn = DBUtil.getConnection();
        String sql = "select * from orders o,business b,orderdetailet l where o.businessId = b.businessId and o.orderId = l.orderId and o.orderId=?";
        pst = conn.prepareStatement(sql);
        pst.setInt(1,orderId);
        Orders orders = new Orders();
        List<OrderDetailet> list = new ArrayList<>();
        try {
            rs = pst.executeQuery();
            while (rs.next()){
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(Integer.parseInt(rs.getString("businessId")));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(Integer.parseInt(rs.getString("orderTypeId")));
                business.setStarPrice(Double.valueOf(rs.getString("starPrice")));
                business.setDeliveryPrice(Double.valueOf(rs.getString("deliveryPrice")));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);


                OrderDetailet detailet = new OrderDetailet();
                detailet.setOdId(rs.getInt("odId"));
                detailet.setOrderId(rs.getInt("orderId"));
                detailet.setFoodId(rs.getInt("foodId"));
                detailet.setQuantity(rs.getInt("quantity"));
                list.add(detailet);
                orders.setList(list);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return orders;
    }

    @Override
    public List<Orders> queryall(String userId) throws SQLException {
        conn = DBUtil.getConnection();
        String sql = "select * from orders o,business b,orderdetailet l where o.businessId = b.businessId and o.orderId = l.orderId and o.userId=?";
        pst = conn.prepareStatement(sql);
        pst.setString(1,userId);
        List<OrderDetailet> list = new ArrayList<>();
        List<Orders> ordersList = new ArrayList<>();
        try {
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(Integer.parseInt(rs.getString("businessId")));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(Integer.parseInt(rs.getString("orderTypeId")));
                business.setStarPrice(Double.valueOf(rs.getString("starPrice")));
                business.setDeliveryPrice(Double.valueOf(rs.getString("deliveryPrice")));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);

                OrderDetailet detailet = new OrderDetailet();
                detailet.setOdId(rs.getInt("odId"));
                detailet.setOrderId(rs.getInt("orderId"));
                detailet.setFoodId(rs.getInt("foodId"));
                detailet.setQuantity(rs.getInt("quantity"));
                list.add(detailet);
                orders.setList(list);
                ordersList.add(orders);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return ordersList;
    }
}

